//
//  YDHBaseItemView.m
//  uda-mail-ios
//
//  Created by 刘权 on 2019/8/28.
//  Copyright © 2019 YunDa. All rights reserved.
//

#import "YDHBaseItemView.h"

@interface YDHBaseItemView()

@property (strong, nonatomic)  NSMutableArray *viewArray;

@end

@implementation YDHBaseItemView

/// 拼接一个子视图
- (void)appendSubView:(UIView *)subView{
    UIView *lastView = [self.viewArray j_objectAtIndex:self.viewArray.count-1];
    [self appendSubView:subView frontView:lastView];
}

/// 在某个子视图后面拼接一个视图
- (void)appendSubView:(UIView *)subView frontView:(UIView *)frontView{
    if (!subView) {
        return;
    }
    NSInteger index = [self.viewArray indexOfObject:frontView];
    if (index == NSNotFound) {
        subView.frame = CGRectMake(subView.v_x, 0.0, subView.v_w, subView.v_h);
        [self addSubview:subView];
        [self.viewArray addObject:subView];
        [self updateHeight];
    }else{
        subView.frame = CGRectMake(subView.v_x, frontView.v_MaxY, subView.v_w, subView.v_h);
        [self addSubview:subView];
        [self.viewArray insertObject:subView atIndex:index+1];
        [self updateAllSubViewFrame];
    }
}

/// 在某个子视图后面拼接一个视图,需要手动调用updateAllSubViewFrame
- (void)appendSubViewWithoutUpdate:(UIView *)subView frontView:(UIView *)frontView{
    if (!subView) {
        return;
    }
    NSInteger index = [self.viewArray indexOfObject:frontView];
    if (index == NSNotFound) {
        subView.frame = CGRectMake(subView.v_x, 0.0, subView.v_w, subView.v_h);
        [self addSubview:subView];
        [self.viewArray addObject:subView];
    }else{
        subView.frame = CGRectMake(subView.v_x, frontView.v_MaxY, subView.v_w, subView.v_h);
        [self addSubview:subView];
        [self.viewArray insertObject:subView atIndex:index+1];
    }
}

/// 删除一个子视图
- (void)removeOneItemView:(UIView *)itemView{
    for (int i = 0; i < _viewArray.count; i++) {
        UIView* view = [_viewArray objectAtIndex:i];
        if (view == itemView) {
            [itemView removeFromSuperview];
            [_viewArray removeObject:itemView];
            [self updateAllSubViewFrame];
            break;
        }
    }
}

/// 删除某个下标的子视图
- (void)removeOnItemViewAtIndex:(NSInteger)index{
    UIView *view = [_viewArray j_objectAtIndex:index];
    if (view != nil) {
        [view removeFromSuperview];
        [_viewArray removeObject:view];
        [self updateAllSubViewFrame];
    }
}

/// 删除所有
- (void)removeAllItemViews{
    for (int i=0; i < _viewArray.count; i++) {
        UIView* view = [_viewArray objectAtIndex:i];
        if (view) {
            [view removeFromSuperview];
        }
    }
    [_viewArray removeAllObjects];
    [self updateAllSubViewFrame];
}

/// 刷新子视图frame
- (void)updateAllSubViewFrame{
    CGFloat lastY = 0.0;
    for (UIView *subView in _viewArray) {
        subView.frame = CGRectMake(subView.v_x, lastY, subView.v_w, subView.v_h);
        lastY = subView.v_MaxY;
    }
    [self updateHeight];
}

- (void)updateHeight{
    UIView *lastView = self.viewArray[self.viewArray.count-1];
    if (lastView) {
        self.v_h = lastView.v_MaxY;
        if (self.heightChangeBlock) {
            self.heightChangeBlock(self.v_h);
        }
    }
}

- (NSArray *)itemViewsArray{
    return [self.viewArray copy];
}

#pragma mark--GETTER
- (NSMutableArray *)viewArray{
    if (!_viewArray) {
        _viewArray = [NSMutableArray array];
    }
    return _viewArray;
}

@end
